<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>QuartzDemo</title>
    <link rel="stylesheet" href="https://unpkg.com/element-ui@2.0.5/lib/theme-chalk/index.css">
    <script src="https://cdn.bootcss.com/vue/2.6.5/vue.common.dev.js"></script>
    <script src="http://cdn.bootcss.com/vue-resource/1.3.4/vue-resource.js"></script>
    <script src="https://unpkg.com/element-ui@2.0.5/lib/index.js"></script>
    <style>
        #top {
            background:#20A0FF;
            padding:5px;
            overflow:hidden
        }
    </style>

</head>
<body>
<div id="test">

    <div id="top">
        <el-button type="text" @click="search" style="color:white">刷新</el-button>
        <el-button type="text" @click="handleadd" style="color:white">添加</el-button>
        </span>
    </div>

    <br/>

    <div style="margin-top:15px">

        <el-table
                ref="testTable"
                :data="tableData"
                style="width:100%"
                border>

<!--            <el-table-column-->
<!--                    prop="id"-->
<!--                    label="id"-->
<!--                    :v-show="false"-->
<!--                    show-overflow-tooltip>-->
<!--            </el-table-column>-->

            <el-table-column
                    prop="name"
                    label="任务名称"
                    sortable
                    show-overflow-tooltip>
            </el-table-column>

            <el-table-column
                    prop="jobGroup"
                    label="任务所在组"
                    sortable>
            </el-table-column>
            <el-table-column
                    prop="jobClassName"
                    label="任务类名"
                    sortable>
            </el-table-column>
            <el-table-column
                    prop="methodName"
                    label="方法名"
                    sortable>
            </el-table-column>
<!--            <el-table-column-->
<!--                    prop="triggerName"-->
<!--                    label="触发器名称"-->
<!--                    sortable>-->
<!--            </el-table-column>-->

<!--            <el-table-column-->
<!--                    prop="triggerGroup"-->
<!--                    label="触发器所在组"-->
<!--                    sortable>-->
<!--            </el-table-column>-->

            <el-table-column
                    prop="cronExpression"
                    label="表达式"
                    sortable>
            </el-table-column>
            <el-table-column
                    prop="cronExpressionVal"
                    label="表达式含义"
                    sortable>
            </el-table-column>
            <el-table-column
                    prop="status"
                    label="状态"
                    sortable>
                <template slot-scope="scope">
                    {{scope.row.status == 1?'已启用':'已停用'}}
                </template>
            </el-table-column>
            <el-table-column label="操作" width="350">
                <template scope="scope">
                    <el-button
                            size="small"
                            type="info"
                            v-if="scope.row.status == 1"
                            @click="handlePause(scope.$index, scope.row)">停用</el-button>

                    <el-button
                            size="small"
                            type="warning"
                            v-if="scope.row.status == 0"
                            @click="handleResume(scope.$index, scope.row)">启用</el-button>

                    <el-button
                            size="small"
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>

                    <el-button
                            size="small"
                            type="success"
                            @click="handleUpdate(scope.$index, scope.row)">修改</el-button>
                    <el-button
                            size="small"
                            type="success"
                            @click="handleTrigger(scope.$index, scope.row)">立即执行</el-button>
                </template>
            </el-table-column>
        </el-table>

        <div align="center">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-sizes="[10, 20, 30, 40]"
                    :page-size="pagesize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="totalCount">
            </el-pagination>
        </div>
    </div>

    <el-dialog title="添加任务" :visible.syn="dialogFormVisible">
        <el-form :model="form">
            <el-form-item label="任务名称" label-width="120px" style="width:80%">
                <el-input v-model="form.name" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="表达式" label-width="120px" style="width:80%">
                <el-input v-model="form.cronExpression" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="任务类名" label-width="120px" style="width:80%">
                <el-input v-model="form.jobClassName" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="方法名" label-width="120px" style="width:80%">
                <el-input v-model="form.methodName" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="任务描述" label-width="120px" style="width:80%">
                <el-input v-model="form.description" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="启用状态" label-width="120px" >
                <el-radio v-model="form.status" label="1">启用</el-radio>
                <el-radio v-model="form.status" label="0">停用</el-radio>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="add">确 定</el-button>
        </div>
    </el-dialog>

    <el-dialog title="修改任务" :visible.syn="updateFormVisible">
        <el-form :model="updateform">
            <el-form-item label="id" label-width="120px" style="display: none">
                <el-input v-model="updateform.id" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="任务名称" label-width="120px" style="width:80%">
                <el-input v-model="updateform.name" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="表达式" label-width="120px" style="width:80%">
                <el-input v-model="updateform.cronExpression" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="任务类名" label-width="120px" style="width:80%">
                <el-input v-model="updateform.jobClassName" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="方法名" label-width="120px" style="width:80%">
                <el-input v-model="updateform.methodName" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="任务描述" label-width="120px" style="width:80%">
                <el-input v-model="updateform.description" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="启用状态" label-width="120px" >
                {{form.status}}
                <el-radio v-model="updateform.status" label="1">启用</el-radio>
                <el-radio v-model="updateform.status" label="0">停用</el-radio>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="updateFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="update">确 定</el-button>
        </div>
    </el-dialog>

</div>

<footer align="center">
    <p>&copy; Quartz 任务管理</p>
</footer>

<script>
    const baseUrl = "http://localhost:8080/";
    var vue = new Vue({
        el:"#test",
        data: {
            //表格当前页数据
            tableData: [],
            //请求的URL
            url:'job/queryjob',
            //默认每页数据量
            pagesize: 10,
            //当前页码
            currentPage: 1,
            //查询的页码
            start: 1,
            //默认数据总数
            totalCount: 1000,
            //添加对话框默认可见性
            dialogFormVisible: false,
            //修改对话框默认可见性
            updateFormVisible: false,
            //提交的表单
            form: {
                name: '',
                jobGroup: '',
                cronExpression: '',
                description: '',
                status: '1',
                triggerName: '',
                triggerGroup: '',
                jobClassName: '',
                methodName: '',
                isNowRun: '0'
            },
            updateform: {
                id: '',
                name: '',
                jobGroup: '',
                cronExpression: '',
                description: '',
                status: '',
                triggerName: '',
                triggerGroup: '',
                jobClassName: '',
                methodName: '',
                isNowRun: '0'
            },
        },
        methods: {
            //从服务器读取数据
            loadData: function(pageNum, pageSize){
                this.$http.get(baseUrl+'jobEntity/list?' + 'pageNum=' +  pageNum + '&pageSize=' + pageSize).then(function(res){
                    var page = res.data;
                    this.tableData = page.records;
                    this.totalCount = page.total;
                },function(){
                    console.log('failed');
                });
            },

            //单行删除
            handleDelete: function(index, row) {
                var than = this;
                this.$http.post(baseUrl+'jobEntity/removeJob/'+row.id,{}).then(function(res){
                    this.loadData( this.currentPage, this.pagesize);
                    if (res.data.code == 200){
                        than.loadData( this.currentPage, this.pagesize);
                        than.$message({message:'操作成功', type: 'success'});
                    }else{
                        than.$message({message: res.data.message, type: 'warning'});
                    }
                },function(){
                    than.$message({message: '请求失败', type: 'error'});
                });
            },

            //暂停任务
            handlePause: function(index, row){
                debugger
                var than = this;
                this.$http.post(baseUrl+'jobEntity/pauseJob/'+row.id,{}).then(function(res){
                    if(res.data.code == 200){
                        than.$message({message:'操作成功', type: 'success'});
                        than.loadData( this.currentPage, this.pagesize);
                    }else{
                        than.$message({message: res.data.message, type: 'warning'});
                    }

                },function(){
                    than.$message({message:'请求失败', type: 'error'});
                });
            },

            //恢复任务
            handleResume: function(index, row){
                var than = this;
                this.$http.post(baseUrl+'jobEntity/resumeJob/'+row.id,{}).then(function(res){
                    if(res.data.code == 200){
                        than.$message({message:'操作成功', type: 'success'});
                        than.loadData( this.currentPage, this.pagesize);
                    }else{
                        than.$message({message: res.data.message, type: 'warning'});
                    }
                },function(){
                    than.$message({message:'请求失败', type: 'error'});
                });
            },
            handleTrigger: function(index,row){
                var than = this;
                this.$http.post(baseUrl+'jobEntity/triggerJob/'+row.id,{}).then(function(res){
                    if(res.data.code == 200){
                        than.$message({message:'操作成功', type: 'success'});
                        than.loadData( this.currentPage, this.pagesize);
                    }else{
                        than.$message({message: res.data.message, type: 'warning'});
                    }
                },function(){
                    than.$message({message:'请求失败', type: 'error'});
                });
            },
            //搜索
            search: function(){
                this.loadData(this.currentPage, this.pagesize);
            },

            //弹出对话框
            handleadd: function(){
                this.dialogFormVisible = true;
            },

            //添加
            add: function(){
                debugger;
                var than = this;
                var postData = {
                    "name": this.form.name,
                    "jobGroup": "",
                    "cronExpression": this.form.cronExpression,
                    "description": this.form.description,
                    "methodName": this.form.methodName,
                    "status": this.form.status,
                    "jobClassName": this.form.jobClassName,
                    "isNowRun": this.form.isNowRun
                }
                this.$http.post(baseUrl +'jobEntity/addJob',postData).then(function(res){

                    if (res.data.code == 200){
                        than.loadData(this.currentPage, this.pagesize);
                        than.dialogFormVisible = false;
                        than.$message({message:'操作成功', type: 'success'});
                    }else{
                        than.$message({message:res.data.message, type: 'warning'});
                    }
                },function(){
                    than.$message({message:'请求失败', type: 'error'});
                });
            },

            //更新
            handleUpdate: function(index, row){
                console.log(row)
                this.updateFormVisible = true;
                this.updateform.name = row.name;
                this.updateform.id = row.id;
                this.updateform.cronExpression = row.cronExpression;
                this.updateform.description = row.description;
                this.updateform.status = row.status;
                this.updateform.jobClassName = row.jobClassName;
                this.updateform.methodName = row.methodName;
                this.updateform.isNowRun = row.isNowRun;
            },

            //更新任务
            update: function(){
                debugger
                var than = this;
                var postData = {
                    "id": this.updateform.id,
                    "name": this.updateform.name,
                    "cronExpression": this.updateform.cronExpression,
                    "description": this.updateform.description,
                    "status": this.updateform.status,
                    "jobClassName": this.updateform.jobClassName,
                    "methodName": this.updateform.methodName ,
                    "isNowRun": this.updateform.isNowRun
                }
                this.$http.post(baseUrl +'jobEntity/updateJob',postData).then(function(res){
                    if (res.data.code == 200){
                        than.loadData(this.currentPage, this.pagesize);
                        than.updateFormVisible = false;
                        than.$message({message:'操作成功', type: 'success'});
                    }else{
                        than.$message({message:res.data.message, type: 'warning'});
                    }

                },function(){
                    than.$message({message:'请求失败', type: 'error'});
                });

            },

            //每页显示数据量变更
            handleSizeChange: function(val) {
                this.pagesize = val;
                this.loadData(this.currentPage, this.pagesize);
            },

            //页码变更
            handleCurrentChange: function(val) {
                this.currentPage = val;
                this.loadData(this.currentPage, this.pagesize);
            },

        },


    });

    //载入数据
    vue.loadData(vue.currentPage, vue.pagesize);
</script>

</body>
</html>